home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / internet-tools / ipdial / source / buffer.c next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  2.9 KB  |  164 lines

  1. /**
  2. ***  IPDial - Buffer.c
  3. ***
  4. ***  This file implements dynamically growing buffers.
  5. ***
  6. ***
  7. ***  This program is free software; you can redistribute it and/or modify
  8. ***  it under the terms of the GNU General Public License as published by
  9. ***  the Free Software Foundation; either version 2 of the License, or
  10. ***  (at your option) any later version.
  11. ***
  12. ***  This program is distributed in the hope that it will be useful,
  13. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ***  GNU General Public License for more details.
  16. ***
  17. ***  You should have received a copy of the GNU General Public License
  18. ***  along with this program; if not, write to the Free Software
  19. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ***
  21. ***  Authors: Jochen Wiedmann <wiedmann@neckar-alb.de>
  22. ***           Stefan Gybas <cab@studbox.uni-stuttgart.de>
  23. **/
  24.  
  25.  
  26.  
  27.  
  28. /**
  29. ***  Include files
  30. **/
  31. #ifndef IPDIAL_H
  32. #include "IPDial.h"
  33. #endif
  34. #include <ctype.h>
  35.  
  36.  
  37. #ifndef MAX
  38. #define MAX(a,b) (((a)>(b))?(a):(b))
  39. #endif
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /**
  46. ***  Type definitions
  47. **/
  48. typedef struct
  49. { STRPTR buf;
  50.   ULONG size;
  51.   ULONG filled;
  52. } Buffer;
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /*** BufferCreate() function
  59. ***
  60. ***  Creates a new buffer.
  61. **/
  62. APTR BufferCreate(VOID)
  63.  
  64. { Buffer *buf;
  65.  
  66.   if ((buf = malloc(sizeof(*buf))))
  67.   { if ((buf->buf = malloc(2048)))
  68.     { buf->size = 2048;
  69.       BufferClear(buf);
  70.     }
  71.     else
  72.     { free(buf);
  73.       buf = NULL;
  74.     }
  75.   }
  76.   return(buf);
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /*** BufferClear() function
  84. ***
  85. ***  This function clears a buffer.
  86. **/
  87. VOID BufferClear(APTR buf)
  88.  
  89. { ((Buffer *) buf)->filled = 0;
  90.   ((Buffer *) buf)->buf[0] = '\0';
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /**
  98. ***  This function adds the given number of bytes to a buffer.
  99. **/
  100. VOID BufferExtend(APTR buffer, const UBYTE* addbuffer, ULONG addsize)
  101.  
  102. { Buffer *buf = buffer;
  103.   ULONG newfilled = buf->filled + addsize;
  104.   extern ULONG EchoMode;
  105.  
  106.   /**
  107.   ***  Allocate a new buffer, if needed.
  108.   **/
  109.   if (newfilled+1 > buf->size)
  110.   { buf->size = MAX(newfilled+1, buf->size)*2;
  111.  
  112.     if (!(buf->buf = realloc(buf->buf, buf->size)))
  113.     { PrintError(0, "not enough memory");
  114.     }
  115.   }
  116.  
  117.   /**
  118.   ***  We are now guaranteed, that the buffer is big enough.
  119.   ***  Copy data and ensure, that we get a valid C string.
  120.   **/
  121.   memcpy(buf->buf + buf->filled, addbuffer, addsize);
  122.   buf->filled += addsize;
  123.   buf->buf[buf->filled] = '\0';
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. /*** BufferCheck() function
  131. ***
  132. ***  Checks, if a buffer contains one of the given strings.
  133. ***  If this is the case, the strings number (beginning with
  134. ***  0) will be returned, -1 otherwise.
  135. **/
  136. LONG BufferCheck(APTR buffer, STRPTR *args)
  137.  
  138. { Buffer *buf = buffer;
  139.   LONG i;
  140.  
  141.   if (buf->filled)
  142.   { for (i = 0;  *args;  ++i, ++args)
  143.     { if (strstr((char *) buf->buf, (char *)  *args))
  144.       { return(i);
  145.       }
  146.     }
  147.   }
  148.   return(-1);
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155. /*** BufferBuffer() function
  156. ***
  157. ***  Returns the string associated to a buffer.
  158. **/
  159. STRPTR BufferBuffer(APTR buf)
  160.  
  161. { return(((Buffer *) buf)->buf);
  162. }
  163.  
  164.